import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

export const routes: Array<RouteRecordRaw> = [
  {
    redirect: "/login",
    path: "/"
  },
  {
    path: "/paint",
    name: "paint",
    component: () => import("../views/paint/layout/ContainerVue.vue")
  },

  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/LoginVue.vue")
  },

  {
    path: "/home",
    name: "home",
    component: () => import("@/views/admin/layout/HomeVue.vue"),
    children: [
      {
        path: "",
        name: "esl",
        component: () => import("@/views/admin/service/home/home.vue")
      },
      {
        path: "/user-settings",
        name: "user-settings",
        component: () => import("../views/admin/service/user-setting/UserSetting.vue")
      },
      {
        path: "/font",
        name: "font",
        component: () => import("../views/admin/service/font/Font.vue")
      },
      {
        path: "/sendBySkin",
        name: "sendBySkin",
        component: () => import("../views/admin/service/sendBySkin/sendBySkin.vue")
      }
    ]
  },
  {
    path: "/:pathMatch(.*)",
    redirect: "/login"
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

interface LocalComponent {
  path: string;
  component: any;
}
export const loadModules = () => {
  // 1.获取所有路由到数组中
  const localRoutes: LocalComponent[] = [];
  const files: Record<string, any> = import.meta.glob("../views/admin/service/*/*.vue", {
    eager: true
  });
  for (const path in files) {
    const module = files[path];
    const regex = /\/([^/]+(?:\/[^/]+)*)\.vue$/;
    const match = path.match(regex);

    if (match) {
      localRoutes.push({
        path: match[1],
        component: module.default
      });
    }
  }

  return localRoutes;
};
export default router;
